package handler

import (
	"github.com/gin-gonic/gin"
	"img-sys/model"
	"img-sys/pkg"
)

func Login(c *gin.Context) {
	Tel := c.PostForm("tel")
	Password := c.PostForm("password")
	var user model.User
	if Tel == "" || Password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "该字段不能为空",
		})
		return
	}
	err := model.DB.Where("tel=?", Tel).Find(&user).Limit(1).Find(&user).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "登录失败",
		})
		return
	}
	if user.Password != pkg.Md5(Password) {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码不正确",
		})
		return
	}
	token := pkg.Token(Tel)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": gin.H{
			"userID": user,
			"token":  token,
		},
	})
}
